//
//  CustomSearchEntranceView.m
//  Btpin
//
//  Created by Vic on 2019/3/16.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "CustomSearchEntranceView.h"

@interface CustomSearchEntranceView ()
{
    UILabel *searchLbl;
    UIImageView *searchIconImgView;
    CGFloat searchTextW;
}
@end

@implementation CustomSearchEntranceView

- (instancetype)initWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor textColor:(UIColor *)textColor placeholer:(NSString *)placeholer target:(id)target {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUIWithFrame:frame backgroundColor:backgroundColor textColor:textColor placeholer:placeholer];
        self.delegate = target;
    }
    return self;
}
- (void)initUIWithFrame:(CGRect)frame backgroundColor:(UIColor *)backgroundColor textColor:(UIColor *)textColor placeholer:(NSString *)placeholer {
    self.bgView = [UIToolsControl initViewWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) backgroundColor:COLOR_OF_WHITE];
    self.bgView.layer.cornerRadius = 15;
    self.bgView.layer.masksToBounds = YES;
    [self addSubview:self.bgView];
    
    
    searchTextW = [UIToolsControl textWidth:placeholer size:CGSizeMake(MAXFLOAT, 30) font:Font_13]+1;
//    UILabel *searchLabel = [UIToolsControl initLabelWithFrame:CGRectMake(EndX(searchIconImageView)+5*Scale, 0, searchTextW, ViewHeight(self.bgView)) text:placeholer textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_13];
    searchLbl = [UIToolsControl initLabelWithFrame:CGRectMake(self.frame.size.width*0.5-20, 0, searchTextW, ViewHeight(self.bgView)) text:placeholer textColor:THEMECOLOR_OF_TEXT_9a9a9a font:Font_13];
    [self.bgView addSubview:searchLbl];
    
//    UIImageView *searchIconImageView = [UIToolsControl initImageViewWithFrame:CGRectMake(25, 9, 13, 14) imageName:@"search_icon_gary"];
    searchIconImgView = [UIToolsControl initImageViewWithFrame:CGRectMake(StartX(searchLbl)-20, 9, 13, 14) imageName:@"search_icon_gary"];
    [self.bgView addSubview:searchIconImgView];
    
    
    UIButton *searchBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, ViewWidth(self.bgView), 30) imageName:nil target:self action:@selector(searchBtnTap) title:nil titleColor:nil];
    [self.bgView addSubview:searchBtn];
}
- (void)searchBtnTap {
    if (self.delegate && [self.delegate respondsToSelector:@selector(customSearchEntranceViewSearchTap)]) {
        [self.delegate customSearchEntranceViewSearchTap];
    }
}
- (void)resetFrame {
    self.bgView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    searchLbl.frame = CGRectMake(self.frame.size.width*0.5-20, 0, searchTextW, ViewHeight(self.bgView));
    searchIconImgView.frame = CGRectMake(StartX(searchLbl)-20, 9, 13, 14);
}

@end
